<?php
/*
 * Created on Dec 27, 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
require_once "../../../mainfile.php";
require_once "../../../include/modules/mysql2json.class.php";

switch ($_GET['action']) {
 case "select_content":
    select_content($_GET['storyId'],$_GET['body']);
    break;
    
 case "submit":
    submit();
    break;    
    
 case "newCat":
    newCategory();
    break;  
    
 case "renameMenuItem":
    renameMenuItem();
    break;      
    
 case "deleteMenuItem":
 	deleteMenuItem();
 	break;
 	
 case "cut":
 	cut();
 	break;
 	
 case "paste":
 	paste();
 	break;
}




function select_content($storyId,$body){
	global $xoopsDB;
	if($body == 'false'){
		$result = $xoopsDB->query("select storyid, title, category from ".$xoopsDB->prefix("content")." WHERE storyid = '".$storyId."'") ;
		if ( $result ) {
			$objJSON=new mysql2json();
			print("/*".trim($objJSON->getJSON($result,$xoopsDB->getAffectedRows($result)))."*/"); 
		}
	}elseif($body == 'true'){
		$result = $xoopsDB->query("select text from ".$xoopsDB->prefix("content")." WHERE storyid = '".$storyId."'") ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			echo $myrow['text'];
		}
	}

}



//-----------------------------------------------------------------------------------------------


function submit(){
	global $xoopsDB;

	if($_POST['editID'] != ""){//process update to existing content
		$query = "update ".$xoopsDB->prefix("content")." set title = '".$_POST['title']."', category = '".$_POST['category']."', text = '".$_POST['FCKBody']."' where storyid = '".$_POST['editID']."'";
		$result = $xoopsDB->query($query);
		if ($result) {
			echo "/*{update:'true',title:'".$_POST['title']."'}*/";
		}else{
			echo "/*{update:\"true\",title:ERROR: \"".$query."\"}*/";
		}
	}elseif($_POST['editID'] == ""){
		$query = "insert into ".$xoopsDB->prefix("content")." values ('0','".$_POST['title']."','".$_POST['FCKBody']."','1','".$_POST['category']."')";
		$result = $xoopsDB->query($query);
		if ($result) {
			echo "/*{update:'false',title:'".$_POST['title']."'}*/";
		}else{
			echo "/*{update:\"false\",title:ERROR: \"".$query."\"}*/";
		}		
	}
		

		
}

//-----------------------------------------------------------------------------------------------

function newCategory(){
	global $xoopsDB;

		$result = $xoopsDB->query("insert into ".$xoopsDB->prefix("content_categories")." values ('0','".$_POST['newCategoryText']."')");
		if ( $result ) {
			$rows = $xoopsDB->getAffectedRows($result);
			if($rows == 1){
				//echo $rows;
				echo "/*{success:'true',category:'".$_POST['newCategoryText']."'}*/";
			}
		}

}

//-----------------------------------------------------------------------------------------------

function renameMenuItem(){
	global $xoopsDB;
	if($_POST['contOrCategory'] == 'category'){
		$result = $xoopsDB->query("update ".$xoopsDB->prefix("content_categories")." set category = '".strtoupper($_POST['renameMenuItem'])."' where category = '".$_POST['oldTitle']."'");
	}elseif($_POST['contOrCategory'] == 'content'){
		$result = $xoopsDB->query("update ".$xoopsDB->prefix("content")." set title = '".$_POST['renameMenuItem']."' where storyid = '".$_POST['renameID']."'");
	}
	
	if ( $result ) {
		$rows = $xoopsDB->getAffectedRows($result);
		if($rows == 1){
			echo "/*{success:'true',category:'".$_POST['renameMenuItem']."'}*/";
		}
	}
}

//-----------------------------------------------------------------------------------------------

function deleteMenuItem(){
	global $xoopsDB;
	if($_POST['contOrCategory'] == 'category'){
	
		//BLAST ALL CONTENT ASSOCIATED WITH SAID CATEGORY
		$result = $xoopsDB->query("delete from  ".$xoopsDB->prefix("content")." where ".$xoopsDB->prefix("content.category")." = (
		select 
			ID 
		from 
			".$xoopsDB->prefix("content_categories")."
		where
			category = '".$_POST['oldTitle']."'
		)");
		
		if ( $result ) {
			$rows = $xoopsDB->getAffectedRows($result);
		}
		
		//FINALLY DELETE THE CATEGORY ITSELF
		$result = $xoopsDB->query("delete from ".$xoopsDB->prefix("content_categories")." where category = '".$_POST['oldTitle']."'");
		if ( $result ) {
			$rows = $xoopsDB->getAffectedRows($result);
			if($rows == 1){
				echo "/*{success:'true'}*/";
			}
		}
	
	}elseif($_POST['contOrCategory'] == 'content'){
		$result = $xoopsDB->query("delete from ".$xoopsDB->prefix("content")." where storyid = '".$_POST['deleteID']."'");
		if ( $result ) {
			$rows = $xoopsDB->getAffectedRows($result);
			if($rows == 1){
				echo "/*{success:'true'}*/";
			}
		}
	}
}
//-----------------------------------------------------------------------------------------------

function cut(){
	global $xoopsDB;
	$result = $xoopsDB->query("update ".$xoopsDB->prefix("content")." set ".$xoopsDB->prefix("content.category")." = (select ID from ".$xoopsDB->prefix("content_categories")." where ".$xoopsDB->prefix("content_categories").".category = '".$_POST['toCategory']."') where storyid = '".$_POST['storyid']."'");
	if ( $result ) {
		$rows = $xoopsDB->getAffectedRows($result);
		if($rows == 1){
			echo "/*{success:'true'}*/";
		}
	}
}


//-----------------------------------------------------------------------------------------------

/*
 * select 
									 ".$xoopsDB->prefix("content").".title AS TITLE
									 ,".$xoopsDB->prefix("content").".text AS TEXT
									 ,".$xoopsDB->prefix("content_catgory")." AS NEW_CAT
								from  
									".$xoopsDB->prefix("content")." 
										inner join ".$xoopsDB->prefix("content_catgory")."
										on ".$xoopsDB->prefix("content").".category = ".$xoopsDB->prefix("content_catgory").".ID
								where 
									".$xoopsDB->prefix("content").".storyid = '".$_POST['storyid']."'
 */

function paste(){
	global $xoopsDB;
	$result = $xoopsDB->query(" select 
	 								".$xoopsDB->prefix("content").".title AS TITLE
	 								,".$xoopsDB->prefix("content").".text AS TEXT
	 								,NEW_CAT.ID AS NEW_CAT_ID
								from  
									".$xoopsDB->prefix("content")."
										inner join (
								
											select
												ID 
											from 
												".$xoopsDB->prefix("content_categories")." 
											where 
												".$xoopsDB->prefix("content_categories").".category = '".$_POST['toCategory']."'
								
										)as NEW_CAT
								
								where 
									".$xoopsDB->prefix("content").".storyid = '".$_POST['storyid']."'");
	if($result){
		$myrow = $xoopsDB->fetchArray($result);
		$title = $myrow['TITLE'];
		$text = $myrow['TEXT'];
		$newCat = $myrow['NEW_CAT_ID'];
	}
//	insert into  xoops_content values(0,'Frank','footer text',true,11)
	$result1 = $xoopsDB->query("insert into ".$xoopsDB->prefix("content")." values(0,'".$title."','".$text."',true,".$newCat.")");
		
	if ( $result1 ) {
		$rows = $xoopsDB->getAffectedRows($result1);
		if($rows == 1){
			echo "/*{success:'true'}*/";
		}
	}else{
		echo "/*{success:'false'}*/";
	}
	
}//end function
?> 
